<?php
/**
 * Single Product Up-Sells
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product, $woocommerce, $woocommerce_loop;

$upsells = $product->get_upsells();

if ( sizeof( $upsells ) == 0 ) return;

$meta_query = $woocommerce->query->get_meta_query();

$count_u = count($upsells);

$args = array(
	'post_type'           => 'product',
	'ignore_sticky_posts' => 1,
	'no_found_rows'       => 1,
	'posts_per_page'      => $posts_per_page,
	'orderby'             => $orderby,
	'post__in'            => $upsells,
	'post__not_in'        => array( $product->id ),
	'meta_query'          => $meta_query
);

$products = new WP_Query( $args );

$woocommerce_loop['columns'] 	= $columns;

$x = 0;
$y = 0;
$z = 0;

if ( $products->have_posts() ) : ?>

	<div id="slide-upsells" class="single-relate product-relate carousel slide">
	
		<div class="widget-title-sidebar">
			<h3><?php _e( 'You may also like', 'woocommerce' ); ?></h3>
			<hr>
		</div>
		<div class="clearfix"></div>
		<div class="carousel-inner">
				<?php while ( $products->have_posts() ) : $products->the_post(); ?>
				<?php global $product, $post, $wpdb, $average; ?>
				<?php 
				$new_product = get_post_meta( $post->ID, 'new_product', true );
				$count = $wpdb->get_var("
				SELECT COUNT(meta_value) FROM $wpdb->commentmeta
				LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
				WHERE meta_key = 'rating'
				AND comment_post_ID = $post->ID
				AND comment_approved = '1'
				AND meta_value > 0
			");

			$rating = $wpdb->get_var("
				SELECT SUM(meta_value) FROM $wpdb->commentmeta
				LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
				WHERE meta_key = 'rating'
				AND comment_post_ID = $post->ID
				AND comment_approved = '1'
			");
			?>
			<?php if( $x % 3 == 0 ){ ?>
			<div class="item <?php if( $x == 0 ){ echo 'active'; } ?>">
			<?php } ?>
					<div class="feature-content">
						<div class="feature-img">
							<?php
								if( $new_product == 'yes' ){
									echo '<span class="new-product"></span>';
								}
							?>
							<?php if(has_post_thumbnail()) {
								the_post_thumbnail('shop_catalog'); 
							} ?>
							<!-- On Sale -->
							<?php if ($product->is_on_sale()) : ?>
								<?php echo apply_filters('woocommerce_sale_flash', '<span class="onsale"></span>', $post, $product); ?>
							<?php endif; ?>
							<!-- add to cart -->
							<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart_feature" method="post" enctype='multipart/form-data'>
								<?php if( ! $product->is_in_stock() ){ ?>						
									<a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="cart"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( ' ', 'woocommerce' ) ); ?></a>
								<?php }else{ ?>
									<button type="submit" class="cart"></button>
								<?php } ?>
							</form>
							<span class="feature-shadow"></span>
						</div>
						<div class="feature-meta">
							<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
							<?php
								if( $count > 0 ){
									$average = number_format($rating / $count, 1);
							?>
							<div class="star"><span style="width: <?php echo ($average*12).'px'; ?>"></span></div>
							<?php } else { ?>
				
								<div class="star"></div>
								
							<?php } ?>
							
							<p><?php echo $product->get_price_html(); ?></p>
						</div>
					</div>
				<?php if( ( $x+1 )%3==0 || ( $x+1 ) ==  $count_u ){?> </div><?php } ?>
				
				<?php $x ++; ?>

				<?php 
					endwhile; // end of the loop. 
					wp_reset_query();
				?>
		</div>
	</div>
	<div class="carousel-indicators">
		<?php while ( $products->have_posts() ) : $products->the_post(); ?>
			<?php if( $y % 3 == 0 ) { ?>
			<span <?php if( $y == 0 ){ echo 'class="active"'; } ?> data-slide-to="<?php echo $z; ?>" data-target="#slide-upsells"> </span>
		<?php
				$z++;
			}
			$y++; 
		?>
		<?php endwhile; ?>
		<?php wp_reset_query(); ?>
	</div>
	

<?php endif;

wp_reset_postdata();
?>
<div class="clearfix"></div>
